const puppeteer = require('puppeteer');
const CREDS = require('./creds');

// async function run() {
//   const browser = await puppeteer.launch();
//   const page = await browser.newPage();
//
//   await page.goto('https://github.com');
//   await page.screenshot({path: './github.png'});
//
//   browser.close();
// }
//
// run();

async function run() {
  const browser = await puppeteer.launch({
    headless: false
  });
  const page = await browser.newPage();
  await page.goto('https://github.com/login');

  // dom element selectors
  const USERNAME_SELECTORLECTOR = await page.$('#login_field');
  const PASSWORD_SELECTOR = await page.$('#password');
  const BUTTON_SELECTOR = await page.$('#login > form > div.auth-form-body.mt-3 > input.btn.btn-primary.btn-block');

  await page.click(USERNAME_SELECTOR)
  await USERNAME_SELECTORLECTOR.type('CREDS.username')

  await page.click(PASSWORD_SELECTOR)
  await page.type(CREDS.password)

  await page.click(BUTTON_SELECTOR)

  await page.waitForNavigation()

  // 搜索结果页处理
  // await page.goto('https://github.com/search?q=john')
  // await page.waitFor(2*1000)
  // const userEle = '.text-normal'
  // const users = await page.evaluate(sel => {
  //   const $els = document.querySelectorAll(sel)
  //   console.log($els);
  // },userEle)


  // browser.close();
}

run();
